<?php
if( !defined('IN_INDEX') )
{
	printError("ACCESS DENIED", "This page is hidden.");
	exit;
}

$task = $_REQUEST['task'];

switch ($task){
	default:
	case 'get_all_user':
		getAllUser();break;
	case 'get_following_user':
		getFollowingUser();break;	
}

function getAllUser(){
	global $db;
	$sql = "SELECT * FROM " . USERS_TABLE . " ORDER BY user_firstname, user_lastname";
	$db->sql_query("SET NAMES 'utf8'");
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error());
	}
	else
	{
		if(count($result) > 0){
			$userList = "";
			while($row = mysql_fetch_array($result)){
				$userList .= "<div style='width: 250px; float: left; margin-left: 0px;'><input onchange='selectedUser(\"userid_".$row['user_id']."\", \"".$row['user_id']."\")' id='userid_".$row['user_id']."' type='checkbox' value='".$row['user_id']."' name='selecteduser[]' /> ".$row['user_firstname']." ".$row['user_lastname']."</div>" ;
			}
			echo $userList;
		}
		//return $db->sql_affectedrows();
		return;
	}
	
	return 0;
}

function getFollowingUser(){
	global $db;
	
	$sqlFollowing = "SELECT friend_user_id FROM ".FRIENDS_TABLE." WHERE user_id=".$_SESSION['user_id']." AND friend_status='accepted'";
	
	if(!$resultTemp = $db->sql_query($sqlFollowing))
	{
		printerror("SQL ERROR", mysql_error());
	}
	else
	{
		if(count($resultTemp) > 0){
			$followingString = "";
			while($rowTemp = mysql_fetch_array($resultTemp)){
				$followingString .= $rowTemp['friend_user_id'].",";
			}
			$followingString =  substr($followingString, 0, strlen($followingString)-1);
			$sql = "SELECT * FROM " . USERS_TABLE." WHERE user_id IN (".$followingString.") ORDER BY user_firstname, user_lastname";
			$db->sql_query("SET NAMES 'utf8'");
			if(!$result = $db->sql_query($sql))
			{
				printerror("SQL ERROR", mysql_error());
			}
			else
			{
				if(count($result) > 0){
					$userList = "";
					while($row = mysql_fetch_array($result)){
						$userList .= "<div style='width: 250px; float: left; margin-left: 0px;'><input onchange='selectedUser(\"userid_".$row['user_id']."\", \"".$row['user_id']."\")' id='userid_".$row['user_id']."' type='checkbox' value='".$row['user_id']."' name='selecteduser[]' /> ".$row['user_firstname']." ".$row['user_lastname']."</div>" ;
					}
					echo $userList;
				}
				//return $db->sql_affectedrows();
				return;
			}
		}
	}
	return 0;
}